//
//  AnimeReadViewController.m
//  GentlemanAnime
//
//  Created by dlios on 15-7-3.
//  Copyright (c) 2015年 lanou3g.com 蓝欧科技. All rights reserved.
//

#import "AnimeReadViewController.h"
#import "HTTPTool.h"
#import "TuCollectionViewCell.h"
#import "UIImageView+WebCache.h"
#import "TuPageModel.h"
#import "TuSizeModel.h"
#import "MBProgressHUD.h"
#import "Singleton.h"
#import "DownloadSingletonTool.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

typedef NS_ENUM(NSInteger, UpViewIsShow) {
    SHOU = 1,
    NOSHOU,
    CHANGING
};

@interface AnimeReadViewController () <UICollectionViewDataSource, UICollectionViewDelegate, SDWebImageManagerDelegate,UIAlertViewDelegate>
@property(nonatomic, retain)UIView *manhuaView;
@property(nonatomic, retain)UICollectionView *collectionView;
@property(nonatomic, retain)NSMutableArray *pageArr;
@property(nonatomic, retain)UILabel *titleLabel;
@property(nonatomic, retain)UILabel *jishuLabel;
@property(nonatomic, retain)UILabel *yeLabel;
@property(nonatomic, retain)UILabel *wangLabel;
@property(nonatomic, retain)UILabel *timeLabel;
@property(nonatomic, retain)UIView *upView;
@property(nonatomic, retain)NSTimer *timer;
@property(nonatomic, retain)TuPageModel *tuPageModel;
@property(nonatomic, retain)NSMutableArray *sizeArr;
@property(nonatomic, assign)NSInteger index;
@property(nonatomic, assign)NSInteger oldShow;
@property(nonatomic, assign)UpViewIsShow isShow;
@property(nonatomic, retain)MBProgressHUD *HUD;
@property(nonatomic, retain)UIView *startView;
@property(nonatomic ,retain)UIView *caozuoView;
@property(nonatomic, retain)UIView *menuView;
@property(nonatomic, retain)UIView *leftView;
@property(nonatomic, retain)UIView *smallLeftView;
@property(nonatomic, retain)UIView *rightView;
@property(nonatomic, retain)UIView *smallRightView;
@property(nonatomic, retain)UIButton *menuButton;
@property(nonatomic, retain)UIButton *leftButton;
@property(nonatomic, retain)UIButton *rightButton;
@property(nonatomic, assign)BOOL isHide;
@property(nonatomic, retain)UIView *shangView;

@end

@implementation AnimeReadViewController

@synthesize leftSwipeGestureRecognizer,rightSwipeGestureRecognizer;

- (void)dealloc
{
    [[SDImageCache sharedImageCache] clearMemory];  
    [_tuPageModel release];
    [_manhuaView release];
    [_collectionView release];
    [_pageArr release];
    [_titleLabel release];
    [_jishuLabel release];
    [_yeLabel release];
    [_wangLabel release];
    [_timeLabel release];
    [_upView release];
    [_sizeArr release];
    [_startView release];
    [_caozuoView release];
    [_menuView release];
    [_leftView release];
    [_smallLeftView release];
    [_rightView release];
    [_smallRightView release];
    [_shangView release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.isShow = SHOU;
        self.isHide = YES;
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor blackColor];
    
    // 小菊花
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.color = [UIColor blackColor];
    self.HUD.mode = MBProgressHUDModeIndeterminate;
    
//    if ((NSInteger)[Singleton shareSingleton].status == AFNetworkReachabilityStatusReachableViaWWAN) {
//        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"消耗流量提醒" message:@"当前为2G/3G/4G网络,继续观看可能会被运营商收取流量费用" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//        [alertView show];
//        [alertView release];
//    }
    
    // 创建一个瀑布流效果
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(WIDTH, HEIGHT - 60);
    flowLayout.minimumLineSpacing = 0;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    
    // 创建CollectionView
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor clearColor];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.pagingEnabled = YES;
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    [self.view addSubview:self.collectionView];
    [_collectionView release];
    
    // 注册
    [self.collectionView registerClass:[TuCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    
    // 创建操作指示面
    self.caozuoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
//    self.caozuoView.alpha = 0;
    self.caozuoView.userInteractionEnabled = YES;
    self.leftSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];
    self.rightSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];
    
    self.leftSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
    self.rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
    [self.caozuoView addGestureRecognizer:self.leftSwipeGestureRecognizer];
    [self.caozuoView addGestureRecognizer:self.rightSwipeGestureRecognizer];
    [self.view addSubview:self.caozuoView];
    [_caozuoView release];
    
    self.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH / 3, HEIGHT)];
//    self.leftView.backgroundColor = [UIColor orangeColor];
    self.leftView.userInteractionEnabled = YES;
    // 创建手势
    UITapGestureRecognizer *leftTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(leftTapAction:)];
    // 把手势添加到指定的控件上
    [self.leftView addGestureRecognizer:leftTap];
    [self.caozuoView addSubview:self.leftView];
    [_leftView release];
    
    self.smallLeftView = [[UIView alloc] initWithFrame:CGRectMake(WIDTH / 3, 0, WIDTH / 3, WIDTH / 3)];
//    self.smallLeftView.backgroundColor = [UIColor orangeColor];
    self.smallLeftView.userInteractionEnabled = YES;
    // 创建手势
    UITapGestureRecognizer *sLeftTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(leftTapAction:)];
    // 把手势添加到指定的控件上
    [self.smallLeftView addGestureRecognizer:sLeftTap];
    [self.caozuoView addSubview:self.smallLeftView];
    [_smallLeftView release];
    
    self.menuView = [[UIView alloc] initWithFrame:CGRectMake(WIDTH / 3, WIDTH / 3, WIDTH / 3, HEIGHT - 2 * WIDTH / 3)];
//    self.menuView.backgroundColor = [UIColor colorWithRed:0.400 green:0.400 blue:1.000 alpha:1.000];
    self.menuView.userInteractionEnabled = YES;
    // 创建手势
    UITapGestureRecognizer *menuTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(menuTapAction:)];
    // 把手势添加到指定的控件上
    [self.menuView addGestureRecognizer:menuTap];
    [self.caozuoView addSubview:self.menuView];
    [_menuView release];
    
    self.rightView = [[UIView alloc] initWithFrame:CGRectMake(WIDTH * 2 / 3, 0, WIDTH / 3, HEIGHT)];
//    self.rightView.backgroundColor = [UIColor colorWithRed:1.000 green:0.400 blue:1.000 alpha:1.000];
    self.rightView.userInteractionEnabled = YES;
    // 创建手势
    UITapGestureRecognizer *rightTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(rightTapAction:)];
    // 把手势添加到指定的控件上
    [self.rightView addGestureRecognizer:rightTap];
    [self.caozuoView addSubview:self.rightView];
    [_rightView release];
    
    self.smallRightView = [[UIView alloc] initWithFrame:CGRectMake(WIDTH / 3, HEIGHT - WIDTH / 3, WIDTH / 3, WIDTH / 3)];
//    self.smallRightView.backgroundColor = [UIColor colorWithRed:1.000 green:0.400 blue:1.000 alpha:1.000];
    self.smallRightView.userInteractionEnabled = YES;
    // 创建手势
    UITapGestureRecognizer *sRightTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(rightTapAction:)];
    // 把手势添加到指定的控件上
    [self.smallRightView addGestureRecognizer:sRightTap];
    [self.caozuoView addSubview:self.smallRightView];
    [_smallRightView release];
    
    
    self.leftButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.leftButton.frame = CGRectMake(WIDTH / 6 - 25, HEIGHT / 2 - 10, WIDTH * 50 / 320, WIDTH * 20 / 320);
    [self.leftButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.leftButton.alpha = 0;
    [self.leftView addSubview:self.leftButton];
    [self.leftButton setTitle:@"上一页" forState:UIControlStateNormal];
    
    self.menuButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.menuButton.frame = CGRectMake(WIDTH / 6 - 15, HEIGHT / 2 - WIDTH / 3 - 10, WIDTH * 30 / 320, WIDTH * 20 / 320);
    [self.menuButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.menuButton.alpha = 0;
    [self.menuView addSubview:self.menuButton];
    [self.menuButton setTitle:@"菜单" forState:UIControlStateNormal];
    
    self.rightButton = [UIButton buttonWithType:UIButtonTypeSystem];    self.rightButton.frame = CGRectMake(WIDTH / 6 - 25, HEIGHT / 2 - 10, WIDTH * 50 / 320, WIDTH * 20 / 320);
    [self.rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.rightButton.alpha = 0;
    [self.rightView addSubview:self.rightButton];
    [self.rightButton setTitle:@"下一页" forState:UIControlStateNormal];
    
    // 判断是否可以请求
    [self canCreateData];
    
    self.manhuaView = [[UIView alloc] initWithFrame:CGRectMake(WIDTH - 160, HEIGHT - 20, WIDTH * 160 / 320, WIDTH * 2 / 32)];
    self.manhuaView.backgroundColor = [UIColor colorWithWhite:0.098 alpha:1.000];
    [self.view addSubview:self.manhuaView];
    [_manhuaView release];
    
    self.jishuLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH * 5 / 320, 0, WIDTH * 5 / 32, WIDTH * 2 / 32)];
    self.jishuLabel.textColor = [UIColor whiteColor];
    self.jishuLabel.alpha = 1;
    self.jishuLabel.font = [UIFont systemFontOfSize:12];
    [self.manhuaView addSubview:self.jishuLabel];
    [_jishuLabel release];
    
    self.yeLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH * 60 / 320, 0, WIDTH * 4 / 32, WIDTH * 2 / 32)];
    self.yeLabel.textColor = [UIColor whiteColor];
    self.yeLabel.alpha = 1;
    self.yeLabel.font = [UIFont systemFontOfSize:12];
    [self.manhuaView addSubview:self.yeLabel];
    [_yeLabel release];
    
    self.wangLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH * 105 / 320, 0, WIDTH * 50 / 320, WIDTH * 2 / 32)];
    self.wangLabel.textColor = [UIColor whiteColor];
    self.wangLabel.alpha = 1;
    self.wangLabel.font = [UIFont systemFontOfSize:12];
    self.wangLabel.text = @"MOBILE";
    [self.manhuaView addSubview:self.wangLabel];
    [_wangLabel release];
    
    
    [self createBiaotiView];
    
   

    
}

#pragma mark 计算页数
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    self.index = scrollView.contentOffset.x / WIDTH;
    self.yeLabel.text = [NSString stringWithFormat:@"%d/%d", self.index + 1, self.tuPageModel.page.count];
}

#pragma mark 绘制标题栏
- (void)createBiaotiView {
    self.upView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, WIDTH * 64 / 320)];
    self.upView.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.700];
    self.upView.clipsToBounds = YES;
    [self.view addSubview:self.upView];
    [_upView release];
    
    // 返回按钮
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeSystem];
    backButton.frame = CGRectMake(0, 25, 44, 44);
    [backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [backButton setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    [backButton setImageEdgeInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
    backButton.tintColor = [UIColor whiteColor];
    [backButton addTarget:self action:@selector(backButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.upView addSubview:backButton];
    
    // 操作提示按钮
    UIButton *caozuoButton = [UIButton buttonWithType:UIButtonTypeSystem];
    caozuoButton.frame = CGRectMake(self.upView.frame.size.width - 70, 27, 60, 44);
    caozuoButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [caozuoButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [caozuoButton setTitle:@"操作提醒" forState:UIControlStateNormal];
    [caozuoButton setImageEdgeInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
    [caozuoButton addTarget:self action:@selector(caozuoButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.upView addSubview:caozuoButton];
    
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH / 2, 24)];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.textColor = [UIColor whiteColor];
    self.titleLabel.center = CGPointMake(WIDTH / 2, 47);
    [self.upView addSubview:self.titleLabel];
    [_titleLabel release];
    
    // 自动隐藏上栏
    self.timer = [NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(hiddenUpView) userInfo:nil repeats:NO];
}

// 判断是否可以请求
- (void)canCreateData {
    if ([DownloadSingleton shareDownloadSingleton].netState == 0) {
        UIAlertView *alertView0 = [[UIAlertView alloc] initWithTitle:@"提示" message:@"当前无网络,请链接网络后重试" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        alertView0.tag = 5000;
        alertView0.delegate = self;
        [alertView0 show];
        [alertView0 release];
    } else if ([DownloadSingleton shareDownloadSingleton].netState == 1) {
        if ([DownloadSingleton shareDownloadSingleton].canSee == 1) {
            UIAlertView *alertView1 = [[UIAlertView alloc] initWithTitle:@"消耗流量提醒" message:@"当前为2G/3G/4G网络,继续观看可能会被运营商收取流量费用" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"继续观看", nil];
            alertView1.tag = 5001;
            alertView1.delegate = self;
            [alertView1 show];
            [alertView1 release]; 
        } else {
            UIAlertView *alertView2 = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请先设置允许移动网络观看" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            alertView2.tag = 5002;
            alertView2.delegate = self;
            [alertView2 show];
            [alertView2 release];
        }
    } else {
        [self createData];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (alertView.tag == 5000) {
        [self.timer invalidate];
        [self dismissViewControllerAnimated:YES completion:^{
            
        }];
    } else if (alertView.tag == 5001) {
        if (buttonIndex == 0) {
            [self.timer invalidate];
            [self dismissViewControllerAnimated:YES completion:^{
                
            }];
        } else {
            [self createData];
        }
    }
}


// 解析数据
- (void)createData
{
    self.pageArr = [NSMutableArray array];
    NSString *strURL = [NSString stringWithFormat:@"http://api.dmzj.com/dynamic/comicread/%@/%@.json", self.epId, self.listId];
    [HTTPTool get1:strURL body:nil httpResultStyle:JSON success:^(id result) {
        NSDictionary *dic = result;
        self.tuPageModel = [[TuPageModel alloc] init];
        [self.tuPageModel setValuesForKeysWithDictionary:dic];
        [_tuPageModel release];
        
        NSArray *arr = dic[@"size"];
        for (NSDictionary *dic in arr) {
            TuSizeModel *tuSizeModel = [[TuSizeModel alloc] init];
            [tuSizeModel setValuesForKeysWithDictionary:dic];
            [self.sizeArr addObject:tuSizeModel];
            [tuSizeModel release];
        }
        
        [self fuzhi];
        [self.collectionView reloadData];
        [self.HUD hide:YES];
    } failure:^(NSError *error) {
        
        
    }];
    
}

#pragma mark 赋值操作
- (void)fuzhi
{
    self.jishuLabel.text = self.tuPageModel.chapter_name;
    self.titleLabel.text = self.readTitle;
    self.yeLabel.text = [NSString stringWithFormat:@"%d/%d", self.index + 1, self.tuPageModel.page.count];
}


#pragma mark 点击返回
- (void)backButtonClick:(UIButton *)button
{
    [self.timer invalidate];
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

#pragma mark 操作提示按钮
- (void)caozuoButtonClick:(UIButton *)button
{
    self.leftView.backgroundColor = [UIColor orangeColor];
    self.smallLeftView.backgroundColor = [UIColor orangeColor];
    self.menuView.backgroundColor = [UIColor colorWithRed:0.400 green:0.400 blue:1.000 alpha:1.000];
    self.rightView.backgroundColor = [UIColor redColor];
    self.smallRightView.backgroundColor = [UIColor redColor];
    self.caozuoView.alpha = 0.5;
    self.leftButton.alpha = 1;
    self.menuButton.alpha = 1;
    self.rightButton.alpha = 1;
    self.shangView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    self.shangView.userInteractionEnabled = YES;
    // 创建手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    // 把手势添加到指定的控件上
    [self.shangView addGestureRecognizer:tap];
    [self.view addSubview:self.shangView];
    [_shangView release];
}

#pragma mark 隐藏标题框
-(void)hiddenUpView
{
    if (self.isShow == SHOU) {
        self.oldShow = self.isShow;
        self.isShow = CHANGING;
        [UIView animateWithDuration:0.5 animations:^{
            self.upView.frame = CGRectMake(0, 0, WIDTH, 0);
        }];
        [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
        [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(changeIsShow) userInfo:nil repeats:NO];
    } else {
        
    }
}

- (void)changeIsShow {
    if (self.oldShow == SHOU) {
        self.isShow = NOSHOU;
    } else {
        self.isShow = SHOU;
    }
}

// 点击方法显示/隐藏上栏
- (void)menuTapAction:(UITapGestureRecognizer *)tap {
    // 判断是否
    if (self.isShow == SHOU) {
        self.oldShow = self.isShow;
        self.isShow = CHANGING;
        [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
        [UIView animateWithDuration:0.5 animations:^{
            self.upView.frame = CGRectMake(0, 0, WIDTH, 0);
            self.upView.alpha = 0;
        }];
        [NSTimer scheduledTimerWithTimeInterval:0.45 target:self selector:@selector(changeIsShow) userInfo:nil repeats:NO];
    } else if (self.isShow == NOSHOU) {
        self.oldShow = self.isShow;
        self.isShow = CHANGING;
        [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
        [UIView animateWithDuration:0.5 animations:^{
            self.upView.frame = CGRectMake(0, 0, WIDTH, 64);
            self.upView.alpha = 1;
        }];
        [NSTimer scheduledTimerWithTimeInterval:0.45 target:self selector:@selector(changeIsShow) userInfo:nil repeats:NO];
        [self.timer invalidate];
        self.timer = [NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(hiddenUpView) userInfo:nil repeats:NO];
    }
}

- (void)tapAction:(UITapGestureRecognizer *)tap {
    self.leftView.backgroundColor = [UIColor clearColor];
    self.smallLeftView.backgroundColor = [UIColor clearColor];
    self.menuView.backgroundColor = [UIColor clearColor];
    self.rightView.backgroundColor = [UIColor clearColor];
    self.smallRightView.backgroundColor = [UIColor clearColor];
    self.leftButton.alpha = 0;
    self.menuButton.alpha = 0;
    self.rightButton.alpha = 0;
    [self.shangView removeFromSuperview];
}

// 点击上一页
- (void)leftTapAction:(UITapGestureRecognizer *)tap
{
    if (0 < self.collectionView.contentOffset.x) {
        [self.collectionView setContentOffset:CGPointMake(self.collectionView.contentOffset.x - WIDTH, 0) animated:YES];
        self.index = self.collectionView.contentOffset.x / WIDTH;
        self.yeLabel.text = [NSString stringWithFormat:@"%d/%d", self.index, self.tuPageModel.page.count];
    }
}

// 点击下一页
- (void)rightTapAction:(UITapGestureRecognizer *)tap
{
    if (self.collectionView.contentOffset.x < (self.tuPageModel.page.count - 1) * WIDTH) {
        [self.collectionView setContentOffset:CGPointMake(self.collectionView.contentOffset.x + WIDTH, 0) animated:YES];
        self.index = self.collectionView.contentOffset.x / WIDTH;
        self.yeLabel.text = [NSString stringWithFormat:@"%d/%d", self.index + 2, self.tuPageModel.page.count];
    }
}

#pragma mark 滑动的方法
- (void)handleSwipes:(UISwipeGestureRecognizer *)sender
{
    if (sender.direction == UISwipeGestureRecognizerDirectionLeft) {
        if (self.collectionView.contentOffset.x < (self.tuPageModel.page.count - 1) * WIDTH) {
            [self.collectionView setContentOffset:CGPointMake(self.collectionView.contentOffset.x + WIDTH, 0) animated:YES];
            self.index = self.collectionView.contentOffset.x / WIDTH;
            self.yeLabel.text = [NSString stringWithFormat:@"%d/%d", self.index + 2, self.tuPageModel.page.count];
        }
    }else if (sender.direction == UISwipeGestureRecognizerDirectionRight){
        if (0 < self.collectionView.contentOffset.x) {
            [self.collectionView setContentOffset:CGPointMake(self.collectionView.contentOffset.x - WIDTH, 0) animated:YES];
            self.index = self.collectionView.contentOffset.x / WIDTH;
            self.yeLabel.text = [NSString stringWithFormat:@"%d/%d", self.index, self.tuPageModel.page.count];
        }
    }
}

#pragma mark collectionView两个协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.tuPageModel.page.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TuCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    NSURL *url = [NSURL URLWithString:self.tuPageModel.page[indexPath.row]];
    UIImage *defaultImage = [UIImage imageNamed:@"漫图2占位图"];
    [cell.tuImageView sd_setImageWithURL:url placeholderImage:defaultImage options:SDWebImageRefreshCached];
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
